#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n + 1);
        vector<int> g(n + 1);
        for (int i = 1; i <= n; i++)
        {
            f[i] = max(nums[i - 1], f[i - 1] + nums[i - 1]);
            g[i] = min(nums[i - 1], g[i - 1] + nums[i - 1]);
        }
        int x = f[1];
        int y = g[1];
        int sum = 0;
        for (int i = 1; i <= n; i++)
        {
            if (x < f[i])
            {
                x = f[i];
            }
            if (y > g[i])
            {
                y = g[i];
            }
            sum += nums[i - 1];
        }
        return sum == y ? x : max(x, sum - y);
    }
};
